package com.profound.blog.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;

import com.profound.blog.bean.Article;

@Repository
public class ArticleDAO {
	
	@Autowired
	private SqlSessionTemplate temp;
	
	public List<Article> listPage(int page,int size){
		List<Article> list = null;
		Map map = new HashMap();
		map.put("from",(page-1)*size);
		map.put("to",page*size);
		list = this.temp.selectList(Article.class.getName()+".listPage",map);
		return list;	
	}
	
	public int getArticleCount(){
		return this.temp.selectOne(Article.class.getName()+".getArticleCount");
	}
	
	public List<Article> listMyPage(int page,int size,int blogId){
		List<Article> list = null;
		Map map = new HashMap();
		map.put("from",(page-1)*size);
		map.put("to",page*size);
		map.put("blogId", blogId);
		list = this.temp.selectList(Article.class.getName()+".listMyPage",map);
		return list;	
	}
	
	public int getMyArticleCount(int blogId){
		return this.temp.selectOne(Article.class.getName()+".getMyArticleCount",blogId);
	}

	public List<Article> listTypePage(int page,int size,int sysArticleTypeId){
		List<Article> list= null;
		Map map = new HashMap();
		map.put("from", (page-1*size));
		map.put("to",page*size);
		map.put("sysArticleTypeId", sysArticleTypeId);
		list = this.temp.selectList(Article.class.getName()+".listTypePage",map);		
		return list;
	}
	
	public int getTypeArticleCount(int sysArticleTypeId){
		return this.temp.selectOne(Article.class.getName()+".getTypeArticleCount",sysArticleTypeId);
	}
	
	public Article findArticle(int articleId){
		return this.temp.selectOne(Article.class.getName()+".findarticle",articleId);
	}
	
	public int addArticle(Article a){
		return this.temp.insert(Article.class.getName()+".add", a);		
	}
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("app.xml");
		ArticleDAO dao = c.getBean(ArticleDAO.class);
		Article a = dao.findArticle(137);
		System.out.println(a);
	}
	
}
